﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;
using System.IO;
using System.Reflection;
using FTPLib;

namespace MedicalDB
{
    public partial class AdaugareImaginiMedicale : UserControl
    {
        private string CNP;
        private string caleFisier = null;
        private string numeFisier = null;
        private string limba;

        public AdaugareImaginiMedicale(string CNP, string limba)
        {
            InitializeComponent();
            Rename(limba);
            this.limba = limba;
            this.CNP = CNP;
        }

        private void btIncarcare_Click(object sender, EventArgs e)
        {
            string appPath = Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().GetName().CodeBase);

            OpenFileDialog fileDiag = new OpenFileDialog();
            fileDiag.InitialDirectory = "C:\\";
            fileDiag.Filter = "All Image Formats (*.bmp;*.jpg;*.jpeg;*.gif)|*.bmp;*.jpg;*.jpeg;*.gif" +
                              "|Bitmaps (*.bmp)|*.bmp" +
                              "|GIFs (*.gif)|*.gif" +
                              "|JPEGs (*.jpg)|*.jpg;*.jpeg";
            fileDiag.FilterIndex = 1;
            if (fileDiag.ShowDialog() == DialogResult.OK)
            {
                if (fileDiag.FileName.LastIndexOf("\\") != 2)
                // LastIndexOf("\\")==2 pentru C:\
                {
                    caleFisier = fileDiag.FileName.Substring(0, fileDiag.FileName.LastIndexOf("\\"));
                    numeFisier = fileDiag.FileName.Substring(fileDiag.FileName.LastIndexOf("\\") + 1);
                }
                else
                {
                    caleFisier = ".";
                    numeFisier = fileDiag.FileName;
                }

                lbNumeFisier.Text = numeFisier;
            }
        }

        private void btSalveaza_Click(object sender, EventArgs e)
        {
            PersistenceManager pm = PersistenceManager.Instance;
            ImaginiMedicale imed = new ImaginiMedicale();

            imed.IdPacient = CNP;
            imed.Tip = tbTipImagine.Text;
            imed.Data = dtpDate.Value;
            imed.DescriereImagine = tbDescriere.Text;
            if (numeFisier != null)
            {
                imed.Locatie = numeFisier;
            }
            else
            {
                MessageBox.Show("Nu s-a ales nici o imagine.",
                                "Eroare", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }

            // Incarca imaginea pe server
            FTP ftpClient;
            int percent;
            try
            {
                ftpClient = new FTP();

                ftpClient.port = Int32.Parse(FormDBMedical.FTPPort);
                ftpClient.server = FormDBMedical.FTPIp;
                ftpClient.user = FormDBMedical.FTPUser;
                ftpClient.pass = FormDBMedical.FTPParola;

                ftpClient.OpenUpload(caleFisier + "\\" + numeFisier,
                                     numeFisier);

                while (ftpClient.DoUpload() > 0)
                {
                    percent = (int)(((ftpClient.BytesTotal) * 100) / ftpClient.FileSize);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Eroare la transferare imagine.\r\nMotiv: " +
                                ex.Message);
                return;
            }

            // Daca s-a terminat cu succes salveaza imaginea si in baza de date.
            pm.Save(imed);

            MessageBox.Show("Imaginea sa salvat cu succes.",
                            "Succes", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }
    }
}
